package com.wanmait.spacesys.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.spacesys.dao.ForgetDAO;
import com.wanmait.spacesys.vo.UserQusetion;

@WebServlet("/ForgetController")
public class ForgetController extends BaseController {

	
	//账号是否存在
	protected void isExist(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");

//		System.out.println(username);
//		HttpSession session=request.getSession();
//		session.setAttribute("forgetname",username);
		boolean flag =  ForgetDAO.getDAO().isName(username);
		PrintWriter out = response.getWriter();
		if(flag) {
			out.print("have");
		}
		else {
			out.print("nohave");
		}
	}
	
	//查找出所有问题后，转发到密保问题页面显示。
	protected void showQuestion(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<UserQusetion> userQusetions =  ForgetDAO.getDAO().findAll();
		request.setAttribute("userQuestion", userQusetions);
		String username = request.getParameter("username");


		HttpSession session=request.getSession();
		session.setAttribute("forgetname",username);
		
		request.getRequestDispatcher("/web/forgetPass.jsp").forward(request, response);
	}
	
	protected void isRight(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String quesIDStr = request.getParameter("quesID");
		
		Integer quesID = Integer.parseInt(quesIDStr);
		String answer = request.getParameter("answer");
		UserQusetion userQusetion =  ForgetDAO.getDAO().isCorrect(quesID,answer);
			PrintWriter out = response.getWriter();
			if(userQusetion!=null) {
				out.print("have");
			}
			else {
				out.print("nohave");
			}
	}


	@Override
	protected void doDefault(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

}
